#include "Quimera_protocol_v1.h"

static bool interrupt=false;
int Message_queue_setup(const int & queue_key)
{
    int id_message_queue;
    key_t key; 
    key = ftok ("/bin/ls", queue_key);
	if (key == (key_t)-1)
	{
		std::cout << "Error al obtener clave para cola mensajes" << std::endl;
		return(EXIT_FAILURE);
	} 
    
        id_message_queue = msgget (key, 0600 | IPC_CREAT);
	if (id_message_queue == -1)
	{
		std::cout << "Error al obtener identificador para cola mensajes" << std::endl;
		return(EXIT_FAILURE);
	}
        
    return (id_message_queue);
}

int main(int argc, char** argv)
{   
    unsigned char               domain;
    struct sockaddr_in          ip_root_serv;
    int                         packer_queue_id;
    int                         unpacker_queue_id;
    int                         i_queue_id;
    
    bzero((char *) &ip_root_serv, sizeof(ip_root_serv));
    message_queue outbound_mail;
    domain=unsigned (char(atoi(argv[2])));
    ip_root_serv.sin_addr.s_addr=inet_addr(argv[3]);
    ip_root_serv.sin_port=htons(QUIMERA_PORT);
    
    bzero((char *) &outbound_mail, sizeof(outbound_mail));
    packer_queue_id=Message_queue_setup(C_PACKER_QUEUE_KEY);
    unpacker_queue_id=Message_queue_setup(C_UNPACKER_QUEUE_KEY);
    i_queue_id=Message_queue_setup(I_QUEUE_KEY);
    
    std::cout<<"Mi dominio: "<<int(domain)<<std::endl;
    std::cout<<"La ip del servidor: "<<inet_ntoa(ip_root_serv.sin_addr)<<std::endl;
    std::cout<<"packer_queue_id: "<<packer_queue_id<<std::endl;
    std::cout<<"unpacker_queue_id: "<<unpacker_queue_id<<std::endl;
    std::cout<<"i_queue_id: "<<i_queue_id<<std::endl;
    
    Quimera_client cli(domain,ip_root_serv,packer_queue_id,i_queue_id);
      
         while(!interrupt){       
             sleep(1); 
             if(msgrcv(unpacker_queue_id, (struct msgbuf *)&outbound_mail,
             sizeof(outbound_mail.package)+sizeof(outbound_mail.cli_addr),HIGHT_PRIORITY,IPC_NOWAIT)>0)
             {
               std::cout<<"Mensaje de alta prioridad"<<std::endl;
               print_output(outbound_mail.package);
               cli.Package_processor(outbound_mail);
             }
         
             else if(msgrcv(unpacker_queue_id, (struct msgbuf *)&outbound_mail,
             sizeof(outbound_mail.package)+sizeof(outbound_mail.cli_addr),MEDIUM_PRIORITY,IPC_NOWAIT)>0)
             {
               std::cout<<"Mensaje de media prioridad"<<std::endl;
               print_output(outbound_mail.package);
               cli.Package_processor(outbound_mail);  
             }
         
             else if(msgrcv(unpacker_queue_id, (struct msgbuf *)&outbound_mail,
             sizeof(outbound_mail.package)+sizeof(outbound_mail.cli_addr),LOW_PRIORITY,IPC_NOWAIT)>0)
             {
               std::cout<<"Mensaje de baja prioridad"<<std::endl;
               print_output(outbound_mail.package);
               cli.Package_processor(outbound_mail);  
             }
           if(outbound_mail.id_message==CLR)
             {
                 interrupt=true;
                 
             
             }  
        
        }
         msgctl (unpacker_queue_id, IPC_RMID, (struct msqid_ds *)NULL);
         return 0;
}
    
}
